<template>
  <a-modal
    title="编辑工序"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="工序名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入工序名称" v-decorator="['name', {rules: [{required: true, message: '请输入工序名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="工序编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入工序编号" v-decorator="['code', {rules: [{required: true, message: '请输入工序编号！'}]}]" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="不良品项"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            :showSearch="true"
            :filter-option="(input, option) =>{
              return option.componentOptions.children[0].elm.data.indexOf(input) >= 0
            }"
            :maxTagCount="4"
            :maxTagTextLength="4"
            mode="multiple"
            style="width: 100%"
            :allowClear="true"
            placeholder="请选择不良品"
            v-decorator="['mulBadItem',{rules: [{ required: true, message: '请选择不良品！' }]}]">
            <a-select-option v-for="(item,index) in mulBadItemData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="报工权限"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <user-select
            mode="multiple"
            placeholder="请选择报工权限"
            v-decorator="['reportRight', { rules: [ {required: false, message: '请选择报工权限！' }]}]"/>
        </a-form-item>
        <a-form-item v-if="jsonEditShow">
          <json-edit
            ref="jsonEdit"
            :fieldProp="fieldProp"
          />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注" v-decorator="['remarks']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { workStepEdit } from '@/api/modular/main/workstep/workStepManage'
  import XEUtils from 'xe-utils'
  import JsonEdit from '@/components/MyComp/JsonEdit'
  import { fieldConfigList } from '@/api/modular/main/fieldconfig/fieldConfigManage'
  import UserSelect from '@/components/MyComp/UserSelect'
  export default {
    components: {
      UserSelect,
      JsonEdit
    },
    data () {
      return {
        fieldProp: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        jsonEditShow: true,
        mulBadItemData: [],
        form: this.$form.createForm(this),
        text: ''
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        // this.text = JSON.parse(record.josn)
        this.dynamicFieldConfig(record.json)
        const mulBadItem = this.$options
        this.mulBadItemData = mulBadItem.filters['dictData']('bad_item')
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              code: record.code,
              id: record.id,
              mulBadItem: record.mulBadItem ? record.mulBadItem.split(';') : [],
              name: record.name,
              remarks: record.remarks,
              reportRight: record.reportRight ? record.reportRight.split(';') : []
            }
          )
          this.$refs.jsonEdit.getEditForm().setValues(JSON.parse(record.josn))
        }, 500)
      },
      // 动态字段赋值
      dynamicFieldConfig(json) {
        this.jsonEditShow = false
        this.fieldProp = {}
        fieldConfigList({ tableName: 'dw_work_step' }).then(res => {
          res.data.forEach(item => {
            // 查询自定义字段
            this.fieldProp[item.fieldIndex] = {
              type: item.fieldType,
              title: item.fieldTitle,
              'x-decorator': 'FormItem',
              // 'x-component': 'a-input',
              'x-decorator-props': {
                labelCol: 6,
                // labelWrap: true,
                wrapperCol: 16
              },
              'x-component-props': {
                // defaultValue: this.text[item.fieldIndex],
                placeholder: '请输入',
                style: {
                  width: '100%'
                }
              }
            }
          })
          this.jsonEditShow = true
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            if (values.mulBadItem) {
              values.mulBadItem = values.mulBadItem.join(';')
            }
            if (values.reportRight) {
              values.reportRight = values.reportRight.join(';')
            }

            values.name = XEUtils.trim(values.name)
            values.code = XEUtils.trim(values.code)
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            this.$refs.jsonEdit.getEditForm().submit((josn) => {
              values.josn = josn
              workStepEdit(values).then((res) => {
                if (res.success) {
                  this.$message.success('编辑成功')
                  this.confirmLoading = false
                  this.$emit('ok', values)
                  this.handleCancel()
                } else {
                  this.$message.error(res.message)//  + res.message
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        if (this.jsonEditShow) {
          this.$refs.jsonEdit.getEditForm().reset()
        }
        this.visible = false
      }
    }
  }

</script>
